<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:category:child:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="childList"
      row-key="dicId"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="dicValue" label="分类名称" width="260"></el-table-column>
      <el-table-column prop="dicSort" label="排序" width="200"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:category:child:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:category:child:add']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleRemove(scope.row)"
            icon="el-icon-delete"
            v-hasPermi="['system:category:child:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="上级菜单" prop="dicPid">
          <treeselect v-model="form.dicPid" disabled="true" :options="dicOptions" :normalizer="normalizer" placeholder="选择上级菜单" />
        </el-form-item>
        <el-form-item label="名称" prop="dicValue">
          <el-input v-model="form.dicValue" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="dicRemark">
          <el-input type="textarea" v-model="form.dicRemark" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="显示排序" prop="dicSort">
          <el-input-number v-model="form.dicSort" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCChild, addCChild, updateCChild, cChildInfo, delChild } from '@/api/system/category'
import Treeselect from '@riophae/vue-treeselect'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: 'childCategory',
  components: {
    Treeselect
  },
  data() {
    return {
      loading: false,
      childList: [],
      form: {},
      rules: {
        dicValue: [
          { required: true, message: '分类名称必填', trigger: 'blur' }
        ]
      },
      open: false,
      dicOptions: [],
      title: null,
      categoryId: this.$route.params.id
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listCChild(this.categoryId).then(response => {
        this.childList = this.handleTree(response.data, 'dicId', 'dicPid')
        this.loading = false
      })
    },
    reset() {
      this.form = {
        dicId: undefined,
        dicPid: undefined,
        dicValue: undefined,
        dicRemark: undefined,
        dicSort: undefined,
        categoryId: this.categoryId
      }
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    handleAdd(row) {
      this.reset();

      if (row !== undefined) {
        this.form.dicPid = row.dicId;
      }

      this.open = true;
      this.title = "添加数据分类";

      listCChild(this.categoryId).then(response => {
        this.dicOptions = this.handleTree(response.data, 'dicId', 'dicPid')
      })
    },
    handleEdit(row) {
      this.reset();

      this.open = true;
      this.title = "编辑数据分类";

      cChildInfo(row.dicId).then(response => {
        this.form = response.data

        if (this.form.dicPid === 0) {
          this.form.dicPid = undefined
        }
      })

      listCChild(this.categoryId).then(response => {
        this.dicOptions = this.handleTree(response.data, 'dicId', 'dicPid')
      })
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }

      return {
        id: node.dicId,
        label: node.dicValue,
        children: node.children
      };
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dicId != null) {
            updateCChild(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCChild(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    handleRemove(row) {
      this.$confirm('是否确认删除名称为"' + row.dicValue + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delChild(row.dicId);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    }
  }
}
</script>
